package com.qzct.controller;

import com.mybatisflex.core.paginate.Page;
import com.qzct.entity.Banner;
import com.qzct.entity.vo.ResultCode;
import com.qzct.entity.vo.ResultVo;
import com.qzct.service.FileService;
import com.qzct.service.impl.FileServiceImpl;
import com.qzct.utils.JwtUtils;
import io.swagger.annotations.ApiImplicitParam;
import org.apache.ibatis.annotations.Param;
import org.checkerframework.checker.units.qual.A;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.qzct.entity.User;
import com.qzct.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  控制层。
 *
 * @author 闽闽
 * @since 2024-12-07
 */
@RestController
@Api(tags = "用户接口")
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private FileService fileService;
    /**
     * 添加。
     */
    @PostMapping("register")
    @ApiOperation("用户注册")
    public ResultVo register(@RequestBody @ApiParam() User user) {
        user.setImage("http://localhost:9000/test/avatar/20250105/2024%E5%B9%B405%E6%9C%8805%E6%97%A510%E6%97%B641%E5%88%8622%E7%A7%926.jpg_1736053804523.jpg");
        Boolean x=  userService.save(user);
        if (x)
            return ResultVo.success("注册成功");
        return ResultVo.err("注册失败");
    }
    /**
     * 根据主键更新。
     */
    @PutMapping("update")
    @ApiOperation("根据主键更新")
    public boolean update(@RequestBody @ApiParam("主键") User user) {
        return userService.updateById(user);
    }

    /**
     * 查询所有。
     */
    @GetMapping("list")
    @ApiOperation("查询所有")
    public List<User> list() {
        return userService.list();
    }

    /**
     * 根据主键获取详细信息。
     */
    @GetMapping("getInfo/{id}")
    @ApiOperation("根据主键获取")
    public User getInfo(@PathVariable @ApiParam("主键") Serializable id) {
        return userService.getById(id);
    }

    /**
     * 分页查询。
     */
    @GetMapping("page")
    @ApiOperation("分页查询")
    public Page<User> page(@ApiParam("分页信息") Page<User> page) {
        return userService.page(page);
    }

    @PostMapping("login")
    @ApiOperation("登录")
    public ResultVo login(@RequestBody User user){
        User u = userService.login(user.getUsername(), user.getPassword());
        String token = JwtUtils.generateToken(u.getUid(),u.getUsername());
        Map<String,Object> map=new HashMap<>();
        map.put("token",token);
        map.put("userinfo",u);
        return  new  ResultVo(ResultCode.LOGIN_SUCCESS,map);
    }
    @PostMapping(value = "/upload/image",consumes = "multipart/form-data")
    @ApiOperation("更换头像")
    public ResultVo imageUploader(@RequestPart(value = "image") MultipartFile image){
        String fileName=image.getOriginalFilename();
        String url = fileService.upLoadImg("avatar",fileName,image);
        if(url.equals("")){
            return ResultVo.err("文件地址空:"+url);
        }
        return  new ResultVo(url);
    }
    @GetMapping("forget")
    @ApiOperation("重置密码")
    public ResultVo forget(String mail) {
        userService.forget(mail);
        return ResultVo.success("成功");
    }
}
